﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.SqlClient;

namespace Hired
{
    /*  A course that is provided by an external provider.
     *  It is assumed that the provider needs to call/e-mail us before we post.
     *  In other words, there is no account for the provider.   */
    public class DBCourse
    {
        //add
        public static bool CreateCourse(Course co) 
        {
            try
            {
                co.CourseID = "tempvalue";
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.AddToCourses(co);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //retrieve
        public static Course RetrieveCourse(string cID) 
        {
            Course course = null;
            using (hireddbEntities db = new hireddbEntities()) 
            {
                    course =
                    (from a in db.Courses
                    where a.CourseID == cID
                    select a).SingleOrDefault();
            }
            return course;
        }

        //update 
        public static bool UpdateCourse(Course co) 
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Course courseToUpdate = RetrieveCourse(co.CourseID);
                    db.Attach(courseToUpdate);
                    courseToUpdate.Provider = co.Provider;
                    courseToUpdate.Title = co.Title;
                    courseToUpdate.Description = co.Description;
                    courseToUpdate.Type = co.Type;
                    courseToUpdate.Timestamp = co.Timestamp;
                    courseToUpdate.Location = co.Location;
                    courseToUpdate.Industry = co.Industry;

                    db.SaveChanges();
                }

                return true;
            }

            catch (Exception) 
            {
                return false;
            }
        }

        //delete
        public static bool DeleteCourse(Course co) 
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Course courseToDelete = RetrieveCourse(co.CourseID);
                    db.Attach(courseToDelete);
                    db.Courses.DeleteObject(courseToDelete);

                    db.SaveChanges();
                }
                return true;
            }

            catch (Exception) 
            {
                return false;
            }
        }
        public static bool DeleteCourseById(string id)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Course courseToDelete = RetrieveCourse(id);
                    db.Attach(courseToDelete);
                    db.Courses.DeleteObject(courseToDelete);

                    db.SaveChanges();
                }
                return true;
            }

            catch (Exception)
            {
                return false;
            }
        }
        public static List<Course> searchCourse(string searchQuery)
        {
            searchQuery = searchQuery.ToUpper();
            using (hireddbEntities db = new hireddbEntities()) 
            {
                var searchResults =
                    from a in db.Courses
                    where
                    a.Provider.ToUpper().Contains(searchQuery)
                    || a.Title.ToUpper().Contains(searchQuery)
                    || a.Description.ToUpper().Contains(searchQuery)
                    || a.Type.ToUpper().Contains(searchQuery)
                    || a.Industry.ToUpper().Contains(searchQuery)
                    || a.Location.ToUpper().Contains(searchQuery)
                    select a;
                return searchResults.ToList<Course>();
            }
        }
        public static List<Course> advancedSearchCourse(List<Course> searchResults, string industry, string location, string exclude) {
            location = location.ToUpper();
            industry = industry.ToUpper();
            List<Course> advancedSearchResults = searchResults;
            //exclude
            for (int i = 0; i < advancedSearchResults.Count; i++)
            {
                Course a = advancedSearchResults[i];
                if (a.Provider.ToUpper().Contains(exclude)
                    || a.Title.ToUpper().Contains(exclude)
                    || a.Description.ToUpper().Contains(exclude)
                    || a.Type.ToUpper().Contains(exclude)
                    || a.Industry.ToUpper().Contains(exclude)
                    || a.Location.ToUpper().Contains(exclude))
                {
                        advancedSearchResults.Remove(a);
                }
            }

            //check location
            for (int i = 0; i < advancedSearchResults.Count; i++)
            {
                Course a = advancedSearchResults[i];
                if (location != "Select a Location...")
                {
                    if (!a.Location.ToUpper().Contains(location))
                    {
                        advancedSearchResults.Remove(a);
                    }
                }
            }
            //check industry
            for (int i = 0; i < advancedSearchResults.Count; i++)
            {
                Course a = advancedSearchResults[i];
                if (industry != "Select an Industry...") {
                    if (!a.Industry.ToUpper().Contains(industry)) {
                        advancedSearchResults.Remove(a);
                    }
                }
            }
            return advancedSearchResults;
        }
    }
}
